﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Slaughters;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Slaughters
{
    /// <summary>
    /// 添加屠宰信息的命令处理
    /// </summary>
    public class AddSlaughterCommandHandlter : IRequestHandler<AddSlaughterCommand, ApiResult<int>>
    {
        /// <summary>
        /// 屠宰仓储
        /// </summary>
        private readonly IBaseRepository<SlaughterModel> slaughterRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="slaughterRep">屠宰仓储</param>
        /// <param name="mapper">映射</param>
        public AddSlaughterCommandHandlter(IBaseRepository<SlaughterModel> slaughterRep,IMapper mapper)
        {
            this.slaughterRep = slaughterRep;
            this.mapper = mapper;
        }


       /// <summary>
       /// 处理
       /// </summary>
       /// <param name="request">请求</param>
       /// <param name="cancellationToken">取消</param>
       /// <returns></returns>
        public async Task<ApiResult<int>> Handle(AddSlaughterCommand request, CancellationToken cancellationToken)
        {

            //添加屠宰信息

            var Slaughter = mapper.Map<SlaughterModel>(request);
            Slaughter.Creatime = DateTime.Now;
            
            await slaughterRep.AddAsync(Slaughter);

            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加屠宰信息成功"

            };
        }
    }
}
